package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
   @Autowired
    private RoleMapper roleMapper;

   public PageInfo query(QueryObject qo){
       PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
       List list = roleMapper.selectList(qo);
       return new PageInfo(list);
   }

    @Override
    public List selectList(QueryObject qo) {
        return roleMapper.selectList(qo);
    }

    @Override
    public Role selectById(Long id) {
      return   roleMapper.selectById(id);
    }

    @Override
    public void save(Role role,Long[] ids) {

        roleMapper.insert(role);
        //在角色增加之后就要维护角色和权限的中间表
        if (ids!=null&&ids.length>0){
            for (long id:ids){
                roleMapper.insertRelation(role.getId(),id);
            }
        }

    }

    @Override
    public void delete(Long id) {
       roleMapper.deleteByPrimaryKey(id);
       roleMapper.deleteRelation(id);
    }

    @Override
    public void update(Role role, Long[] ids) {
       roleMapper.updateByPrimaryKey(role);
       roleMapper.deleteRelation(role.getId());
        if (ids!=null&&ids.length>0){
            for (long id:ids){
                roleMapper.insertRelation(role.getId(),id);
            }
        }

    }

    @Override
    public List selectByRoleId(Long id) {
        return roleMapper.selectByRoleId(id);
    }

    @Override
    public List selectAll() {
      return  roleMapper.selectAll();
    }

}
